package io.renren.utils.util;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.util.Hashtable;

import org.apache.log4j.Logger;

public class Error {
	
	static Logger logger = Logger.getLogger(Error.class.getName());
	static Hashtable<Integer, Error> errorMap = new Hashtable<Integer, Error>();
		
	// 数据库操作基本事件消息
	//99XXXX- 系统级错误(通用错误、数据库访问错误、网络连接错误等)
	public static final int EVENT_MODULE_FRAMEWORK = 99;	
	
	// 991000-未知系统错误
	public final static Error SYS_UNKNOWN_ERROR = new Error(991000, "SYS_UNKNOWN_ERROR"); // 			未知系统错误
	
	// 991XXX –DB操作
	public final static Error SYS_NO_CONNECTION = new Error(991001, "SYS_NO_CONNECTION"); // 无数据库链接
	public final static Error SYS_SQL_ERROR = new Error(991002, "SYS_SQL_ERROR"); // SQL访问错误
	public final static Error SYS_NO_RESULT = new Error(991003, "SYS_NO_RESULT"); // 无无记录集
	public final static Error SYS_OPEN_CFG_FILE_ERROR = new Error(991004, "SYS_OPEN_CFG_FILE_ERROR"); // 配置文件打开错误
	public final static Error SYS_OPEN_CONNECTION_ERROR = new Error(991005, "SYS_OPEN_CONNECTION_ERROR"); // 打开数据库连接错误
	public final static Error SYS_CLOSE_CONNECTION_ERROR = new Error(991006, "SYS_CLOSE_CONNECTION_ERROR"); // 关闭数据库连接错误
	public final static Error SYS_ERROR_KEY = new Error(991007, "SYS_ERROR_KEY"); // 错误的Key
	public final static Error SYS_SECURITY_ERROR = new Error(991008, "SYS_SECURITY_ERROR"); // 安全错误
	
	// 992XXX –DMT系统错误
	public final static Error DMT_INSERT_ERROR= new Error(992001, "DMT_INSERT_ERROR"); // 		   插入记录错误
	public final static Error DMT_FETCH_ERROR = new Error(992002, "DMT_FETCH_ERROR"); // 			读记录错误
	public final static Error DMT_UPDAET_ERROR = new Error(992003, "DMT_UPDAET_ERROR"); // 			更新记录错误
	public final static Error DMT_DELETE_ERROR = new Error(992004, "DMT_DELETE_ERROR"); //			删除记录错误
	public final static Error DMT_FRESH_ERROR = new Error(992005, "DMT_FRESH_ERROR"); // 			刷新记录错误

	// 993XXX-OMT系统错误
	public final static Error OMT_STORE_ERROR = new Error(993001, "OMT_STORE_ERROR"); // 			保存记录错误
	public final static Error OMT_LOAD_ERROR = new Error(993002, "OMT_LOAD_ERROR"); // 				读记录错误
	public final static Error OMT_REMOVE_ERROR = new Error(993003, "OMT_REMOVE_ERROR"); // 			删除记录错误

	
	///////////////////
	private int errorCode;
	private String errorMessage;

	static {
		combineErrorMap(Error.class.getName());
	}
	

	public Error(int errorCode, String errorMessage) {
		this.errorCode = errorCode;
		this.errorMessage = errorMessage;
	}

	public int getErrorCode() {
		return errorCode;
	}

	public String getErrorMessage() {
		return errorMessage;
	}

	public String toString() {
		return "Event ("+getErrorCode()+"): "+ getErrorMessage();
	}
	
	
	@SuppressWarnings("unchecked")
	protected static void combineErrorMap (String errorClassName) {
		try {
			Class<?> cls = Class.forName(errorClassName);
			//Object error = cls.newInstance();
			Constructor<?> constructor = null;
			while (constructor==null) {
				try {
					constructor = cls.getConstructor(Integer.TYPE, String.class);
				} catch (Exception ex) {
					//ignore
					cls = cls.getSuperclass();
					if (cls == null) {
						break;
					}
				}
			}
			
			constructor.setAccessible(true);
			Object[] arg= new Object[] {0,null}; 
			Object error = constructor.newInstance(arg); 
			
			Field[] fields = error.getClass().getDeclaredFields();
			Field field = null;
			Object obj = null;
			for (int i = 0; i < fields.length; i++) {
				field = fields[i];
				field.setAccessible(true);
				obj = field.get(error);
				if (obj instanceof Error) {
					errorMap.put(((Error)obj).getErrorCode(), (Error)obj);
				}
			}
		} catch (Exception ex) {
			logger.error("Can not combine static error from " + errorClassName, ex);
		}
	}


}
